<template>
  <el-card class="box-card">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <a href="/home" onclick="return false" @click="back">首页</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>资料设置</el-breadcrumb-item>
      <el-breadcrumb-item>基本信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="用户名：">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="性别：">
        <el-select v-model="form.gender">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="个人简介：">
        <el-input
          type="textarea"
          :autosize="{ minRows: 10, maxRows: 15 }"
          v-model="form.introduce">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateUserInfo">保存</el-button>
        <el-button @click="getform">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="40%"
      top="25vh"
      center
      :close-on-press-escape="false"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <h1 class="dialogContent">您的用户名已修改，请您重新登录。</h1>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClick">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: 'Info',
  data () {
    return {
      userInfo: {},
      centerDialogVisible: false,
      form: {
        username: '',
        introduce: '',
        gender: -1
      },
      options: [
        {
          value: -1,
          label: '保密'
        },
        {
          value: 0,
          label: '男'
        },
        {
          value: 1,
          label: '女'
        }
      ]
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    back () {
      this.$router.push('/home')
      this.$store.state.activePath = '/home'
    },
    getform () {
      this.form.username = this.userInfo.username
      this.form.introduce = this.userInfo.introduce
      this.form.gender = this.userInfo.gender
    },
    async getUserInfo () {
      const { data: res } = await this.$http.get('/userInfo')
      if (res.status !== 200) return this.$message.error('获取用户信息失败！')
      this.userInfo = res.data
      this.getform()
    },
    async updateUserInfo () {
      const { data: res } = await this.$http.post('/updateUserInfo', this.form)
      if (res.status !== 200) return this.$message.error('修改用户信息失败！')
      if (this.form.username !== this.userInfo.username) this.centerDialogVisible = true
      this.$message.success('修改用户信息成功')
      this.getUserInfo()
      this.$store.state.activePath = '/basicinfo'
    },
    handleClick () {
      this.$router.push('/')
      this.$store.state.activePath = '/home'
    }
  }
}
</script>

<style lang="less" scoped>
  .el-form {
    .el-form-item {
      margin-top: 50px;

      .el-input {
        width: 50%;
      }

      .el-textarea {
        width: 50%;
      }
    }
  }

  .dialogContent {
    font-size: 30px;
    margin-left: 120px;
  }
</style>
